﻿using log4net;
using SuperSocket.SocketBase;
using SuperSocket.SocketEngine;
using System.ServiceProcess;

namespace IOA.MES.TcpService
{
    public partial class Service1 : ServiceBase
    {
        private IBootstrap bootstrap;
        private readonly ILog logger = LogManager.GetLogger("IOA.MES.TcpService");
        public Service1()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            logger.Info("正在启动服务...");

            bootstrap = BootstrapFactory.CreateBootstrap();
            if (!bootstrap.Initialize())
            {
                logger.Fatal("初始化TCP服务失败！");
            }
            else
            {
                var result = bootstrap.Start();
                logger.Info($"TCP服务启动: {result}");
                if (result == StartResult.Failed)
                {
                    logger.Fatal("启动TCP服务失败！");
                }
                logger.Info($"TCP服务启动成功！");
            }

            logger.Info("服务启动完成...");
        }

        protected override void OnStop()
        {
            logger.Info("正在停止服务...");
            bootstrap.Stop();
            logger.Info("服务停止完成");
        }
    }
}
